<!DOCTYPE html>
<html>

	<head>

		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">


		<title>后台扫描</title>
		<meta name="keywords" content="Vulnerability scanner">
		<meta name="description" content="maverick0407">

		<link rel="shortcut icon" href="../../static/favicon.ico">
		<link href="../../static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
		<link href="../../static/css/font-awesome.css?v=4.4.0" rel="stylesheet">
		<link href="../../static/css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
		<link href="../../static/css/animate.css" rel="stylesheet">
		<link href="../../static/css/style.css?v=4.1.0" rel="stylesheet">


	</head>

	<body class="gray-bg">
		<div class="wrapper wrapper-content animated fadeInRight">
			<div class="ibox float-e-margins">
				<div class="ibox-title">
					<h5>请输入url
					</h5>
					<div class="ibox-tools">
						<a class="collapse-link">
							<i class="fa fa-chevron-up"></i>
						</a>

					</div>
				</div>
				<!--			url输入框开始-->
				<div class="ibox-content">
					<form id="scan-form" method="POST" action="{% url 'dirscan' %}">
						{% csrf_token %}
						<div class="form-group">
							<label for="url_input">URL:</label>
							<input type="text" class="form-control" id="url_input" name="url_input" required>
						</div>
						<button type="submit" class="btn btn-primary" id="scan-button">开始扫描</button>
					</form>
													<!-- 进度条-->
							<div class="progress-div">
								<div class="progress">
									<div class="progress-bar progress-bar-striped active" id="progress-bar" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="100" style="min-width: 0em; width: 02%;">
										0%
									</div>
								</div>
							</div>



							<div class="progress-text progress-bar-striped active" role="progressbar" aria-valuenow="2" aria-valuemin="0" aria-valuemax="100" style="min-width: 2em; width: 2%;">

							</div>
					<!-- 进度条-->
				</div>
				<!--			url输入框结束-->

			</div>

			<!-- 提示信息元素 -->
			<div id="messages">

				{% for message in messages %}
				<div class="alert alert-{{ message.tags }} alert-dismissible" role="alert">
					<button type="button" class="close" data-dismiss="alert" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					{{ message }}
				</div>
				{% endfor %}
			</div>
			<!-- 提示信息元素 -->


			<!-- Panel Other -->
			<div class="ibox float-e-margins">
				<div class="ibox-title">
					<h5>扫描结果</h5>
					<div class="ibox-tools">
						<a class="collapse-link">
							<i class="fa fa-chevron-up"></i>
						</a>

					</div>
				</div>
				<div class="ibox-content">
					<div class="row row-lg">
						<div class="col-sm-12">
							<!-- Example Toolbar -->
							<div class="example-wrap">

								<div class="example">
									<table id="exampleTableToolbar" data-mobile-responsive="true">
										<thead>
											<tr>
												<th data-field="target_url" style="text-align: center;" >目标域名或ip</th>
												<th data-field="response_code" style="text-align: center;" >响应码</th>
												<th data-field="access_status" style="text-align: center;" >连接状态</th>
												<th data-field="scan_time" style="text-align: center;" >时间</th>
												<th data-field="scan_path" style="text-align: center;" >路径</th>
											</tr>
										</thead>
									</table>
								</div>
							</div>
							<!-- End Example Toolbar -->
						</div>


					</div>
				</div>
			</div>
			<!-- End Panel Other -->
		</div>

		<!-- 全局js -->
		<script src="../../static/js/jquery.min.js?v=2.1.4"></script>
		<script src="../../static/js/bootstrap.min.js?v=3.3.6"></script>

		<!-- 自定义js -->
		<script src="../../static/js/content.js?v=1.0.0"></script>


		<!-- Bootstrap table -->
		<script src="../../static/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
		<script src="../../static/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
		<script src="../../static/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>

		<!-- Peity -->
		<script src="../../static/js/demo/bootstrap-table-demo.js"></script>
		<!-- 获取数据 -->
		<script>
			$(document).ready(function() {
						    var intervalId = setInterval(function() {
						        $.ajax({
						            url: "{% url 'dirscan_progress' %}",
						            type: "POST",
						            dataType: "json",
						            success: function(data) {
						                //console.log(data);

						                if (data.task_state == 'SUCCESS!!!') {
						                	var $table = $('#exampleTableToolbar');
						                    //console.log('Task succeeded!');
						                    var dir_data = JSON.parse(data.dir_data);
						                    var table_data = [];
						                    //console.log(dir_data)
											// 遍历“dir_data”数组中的每个元素
											for (var i = 0; i < dir_data.length; i++) {
											  // 从当前元素中获取“fields”属性的值
											  var fields = dir_data[i].fields;
											  // 创建一个新对象，其属性名与表格列的"data-field"属性值匹配
											  var row = {
												target_url:fields.target_url,
												response_code:fields.response_code,
												access_status:'<span class="label label-primary">'+fields.access_status+'</span>',
												scan_time:fields.scan_time,
												scan_path:fields.scan_path,
											  };

											  // 将新对象添加到表格数据数组中
											  table_data.push(row);
											}
											$table.bootstrapTable('load', table_data);
											//修改进度条宽度和文本内容
											$("#progress-bar").css('width', 1700 + 'px');
											$("#progress-bar").text("100% 扫描完成");
						                    clearInterval(intervalId);  // 停止定时器
						                }

						                else if (data.task_state == 'PENDING!!!') {
						                    console.log('Task is still running.');
											//获取当前进度条宽度和文本内容
											var currentWidth = $("#progress-bar").width();
											var currentText = $("#progress-bar").text();
											console.log(currentWidth)
											//增加10%的宽度和文本内容
											var newWidth = currentWidth + $("#progress-bar").parent().width() * 0.01;
											var newText = (newWidth / $("#progress-bar").parent().width() * 100).toFixed(0) + '%';
											console.log(newWidth)
											//修改进度条宽度和文本内容
											$("#progress-bar").css('width', newWidth + 'px');
											$("#progress-bar").text(newText);
						                }
						            },
						            error: function(jqXHR, textStatus, errorThrown) {
						                console.log("AJAX Error: " + textStatus + ", " + errorThrown);
						                // TODO: 处理请求失败的情况
						            }
						        });
						    }, 3000);
						});
		</script>
	</body>

</html>